package com.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.OrderMapper;
import com.pojo.*;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Sans
 */
@Service
public class OrderService {
    
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ClientService clientService;
    @Resource
    private ProductService productService;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public Order queryByKey(int o_num){
        return orderMapper.selectByPrimaryKey(o_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<Order> queryByType(Order order){
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        if (order != null){
            if (order.getO_id() != null && !"".equals(order.getO_id().trim())) {
                criteria.andO_idEqualTo(order.getO_id().trim());
            }
            if (order.getP_id() != null && !"".equals(order.getP_id().trim())) {
                criteria.andP_idEqualTo(order.getP_id().trim());
            }
        }
        return orderMapper.selectByExample(example);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public PageInfo<Order> queryByPage(Integer PageNum , Integer PageSize , QueryVo vo) {
        
        //约束查询
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        if (vo != null) {
            if (vo.getO_id() != null && !"".equals(vo.getO_id().trim())) {
                criteria.andO_idEqualTo(vo.getO_id().trim());
            }
            //c_name 需要查id
            if (vo.getC_name() != null && !"".equals(vo.getC_name().trim())) {
                Client client = null;
                Client tamp = new Client();
                tamp.setC_name(vo.getC_name());
                List<Client> clients = clientService.queryByType(tamp);
                if (!clients.isEmpty()){
                    client = clients.get(0);
                }else {
                    client = new Client();
                    client.setC_id(-1);
                }
                criteria.andC_idEqualTo(client.getC_id());
            }
        }
        
        //填充数据
        PageHelper.startPage(PageNum,PageSize);
        List<Order> list = orderMapper.selectByExample(example);
        for (Order o : list) {
            
            //补充 c_name
            Client c = new Client();
            Client client = null;
            c.setC_id(o.getC_id());
            List<Client> clients = clientService.queryByType(c);
            if (!clients.isEmpty()) {
                client = clients.get(0);
            }
            if (client != null) {
                o.setC_name(client.getC_name());
            }
            
            //补充 p_name
            Product product = null;
            List<Product> products = productService.queryByType(new Product(o.getP_id()));
            if (!products.isEmpty()){
                product = products.get(0);
            }
            if (product != null){
                o.setP_name(product.getP_name());
            }
        }
        
        list.forEach(order -> System.out.println("\torder : " + order));
        
        return new PageInfo<>(list);
    }
    
    @Transactional (propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int add(Order order) {
        if (order == null) {
            return 0;
        }
        return orderMapper.insertSelective(order);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int del(Integer o_num) {
        return orderMapper.deleteByPrimaryKey(o_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int update(Order order) {
        return orderMapper.updateByPrimaryKeySelective(order);
    }
    
}
